

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料 SPOC 运行环境  windows10 JDK8 IDEA 2021.1 专业版 Spring 5.0.5  学习任务：  使用多种方式实现依赖注入 使用多种方式实现AOP Spring 整合 MyBatis学习目标： 理解构造注入 了解p命名空间注入 理解更多增强类型的使用方法 掌握使用注解实现Ioc的方法 掌握使用注解实现AOP的方法  一、Spring IoC控制反">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP">
<meta property="og:url" content="http://example.com/2022/01/20/Spring%20%E7%AC%94%E8%AE%B02%20%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E7%9A%84%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F%E6%9E%84%E9%80%A0%E6%B3%A8%E5%85%A5%E3%80%81p%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E6%B3%A8%E5%85%A5%E7%AD%89%20_%20AOP%E5%A4%9A%E7%A7%8D%E5%A2%9E%E5%BC%BA%E7%B1%BB%E5%9E%8B%E5%BC%82%E5%B8%B8%E6%8A%9B%E5%87%BA%E5%A2%9E%E5%BC%BA%E3%80%81%E7%8E%AF%E7%BB%95%E5%A2%9E%E5%BC%BA%E7%AD%89%20_%20%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91%E5%AE%9E%E7%8E%B0%20IoC_DI%20%E5%92%8C%20AOP/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料 SPOC 运行环境  windows10 JDK8 IDEA 2021.1 专业版 Spring 5.0.5  学习任务：  使用多种方式实现依赖注入 使用多种方式实现AOP Spring 整合 MyBatis学习目标： 理解构造注入 了解p命名空间注入 理解更多增强类型的使用方法 掌握使用注解实现Ioc的方法 掌握使用注解实现AOP的方法  一、Spring IoC控制反">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fb4a5d94600b412ba51c0d80d1d176ce.png">
<meta property="article:published_time" content="2022-01-20T06:40:54.000Z">
<meta property="article:modified_time" content="2022-08-22T15:48:23.831Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/fb4a5d94600b412ba51c0d80d1d176ce.png">
  
  
  
  <title>Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-20 14:40" pubdate>
          2022年1月20日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          13k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          107 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="http://spoc.wzu.edu.cn/study/initplay/787754.mooc">SPOC</a></p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>windows10</li>
<li>JDK8</li>
<li>IDEA 2021.1 专业版</li>
<li>Spring 5.0.5</li>
</ul>
<p>学习任务：</p>
<ul>
<li>使用多种方式实现依赖注入</li>
<li>使用多种方式实现AOP</li>
<li>Spring 整合 MyBatis<br>学习目标：</li>
<li>理解构造注入</li>
<li>了解p命名空间注入</li>
<li>理解更多增强类型的使用方法</li>
<li>掌握使用注解实现Ioc的方法</li>
<li>掌握使用注解实现AOP的方法</li>
</ul>
<h1 id="一、Spring-IoC控制反转-x2F-DI依赖注入-扩展"><a href="#一、Spring-IoC控制反转-x2F-DI依赖注入-扩展" class="headerlink" title="一、Spring IoC控制反转&#x2F;DI依赖注入 扩展"></a>一、Spring IoC控制反转&#x2F;DI依赖注入 扩展</h1><hr>
<h2 id="1-1-构造注入"><a href="#1-1-构造注入" class="headerlink" title="1.1 构造注入"></a>1.1 构造注入</h2><hr>
<p><strong>标签：</strong><code>&lt;constructor-arg&gt;</code></p>
<p>首先回顾上次学习中的注入方法，以业务层的代码为例：<br><code>UserServiceImpl.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span>&#123;<br>	<span class="hljs-keyword">private</span> UserDao userDao;<br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setUserDao</span><span class="hljs-params">(UserDao userDao)</span>&#123;<br>		<span class="hljs-built_in">this</span>.userDao = userDao;<br>	&#125;<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addUser</span><span class="hljs-params">(User user)</span>&#123;<br>		userDao.add(user);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><code>applicationContext.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.uni.dao.UserDaoImpl&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.uni.service.UserServiceImpl&quot;</span>&gt;</span><br>       <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;userDao&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>这里是通过setUserDao方法进行注入的，那么如何通过构造注入实现userDao的实例化呢？答案是在UserServiceImpl的构造方法里声明这个参数并赋值。比如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title function_">UserServiceImpl</span><span class="hljs-params">(UserDao userDao)</span>&#123;<br>	<span class="hljs-built_in">this</span>.userDao = userDao<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.uni.dao.UserDaoImpl&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.uni.service.UserServiceImpl&quot;</span>&gt;</span><br>       <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">&quot;0&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;userDao&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>特点：</strong></p>
<ul>
<li>不用setter方法</li>
<li>构造对象时注入</li>
</ul>
<p>注：编写带参构造方法后，JVM不再提供默认的无参构造，为了保证使用的灵活性，建议自行添加一个无参构造，如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title function_">UserServiceImpl</span><span class="hljs-params">()</span>&#123;&#125;<br></code></pre></td></tr></table></figure>

<p>构造注入和设值注入的比较：</p>
<table>
<thead>
<tr>
<th align="left">设值注入</th>
<th align="left">构造注入</th>
</tr>
</thead>
<tbody><tr>
<td align="left">通过setter访问器实现</td>
<td align="left">通过构造方法实现</td>
</tr>
<tr>
<td align="left">灵活性好，但setter方法属性较多</td>
<td align="left">灵活性差，仅靠重载限制太多</td>
</tr>
<tr>
<td align="left">时效性差</td>
<td align="left">时效性好</td>
</tr>
<tr>
<td align="left">通过无参构造实例化</td>
<td align="left">通过匹配的构造方法实例化，但建议保留无参构造</td>
</tr>
</tbody></table>
<h2 id="1-2-使用p命名空间注入属性值"><a href="#1-2-使用p命名空间注入属性值" class="headerlink" title="1.2 使用p命名空间注入属性值"></a>1.2 使用p命名空间注入属性值</h2><p><strong>特点：</strong>使用<strong>属性</strong>而不是子标签的形式配置Bean的属性，简化了配置代码。</p>
<p><strong>基本语法：</strong></p>
<ul>
<li>对于直接量（基本属性类型、字符串）属性：<code>p:属性名=&quot;属性值&quot;</code></li>
<li>对于引用Bean的属性：<code>p:属性名-ref=&quot;Bean的id&quot;</code></li>
</ul>
<p>使用前要先在Spring配置文件中引入p命名空间<br><code>applicationContext.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml">xmlns:p=&quot;http://www.springframework.org/schema/p&quot;<br></code></pre></td></tr></table></figure>
<p>案例：使用p命名空间注入属性值</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> = <span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span> <span class="hljs-attr">p:age</span>=<span class="hljs-string">&quot;21&quot;</span> <span class="hljs-attr">p:username</span>=<span class="hljs-string">&quot;uni&quot;</span>/&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> = <span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> = <span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span> <span class="hljs-attr">p:userDao-ref</span>=<span class="hljs-string">&quot;userDao&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="1-3-注入不同的数据类型"><a href="#1-3-注入不同的数据类型" class="headerlink" title="1.3 注入不同的数据类型"></a>1.3 注入不同的数据类型</h2><ol>
<li>注入直接量<br>使用<code>&lt;value&gt;</code>标签实现，注意特殊字符的处理</li>
<li>引用 Bean<br>  使用<code>&lt;ref&gt;</code>标签实现，注意<code>bean</code>属性和<code>local</code>属性的区别</li>
<li>使用内部 Bean <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span> = <span class="hljs-string">&quot;dao&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;dao.impl.UserDaoImpl&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
<li>注入集合类型的属性<br>分别使用<code>&lt;list&gt;</code>、<code>&lt;set&gt;</code>、<code>&lt;map&gt;</code>、<code>&lt;props&gt;</code>标签实现</li>
<li>注入 null 和 空字符串值<br>使用<code>&lt;null/&gt;</code>注入 null 值<br>使用 <code>&lt;value&gt;&lt;/value&gt;</code>注入空字符串值</li>
</ol>
<h2 id="1-4-XML注入特殊符号"><a href="#1-4-XML注入特殊符号" class="headerlink" title="1.4 XML注入特殊符号"></a>1.4 XML注入特殊符号</h2><p>特殊符号比如：<code>&lt; &amp; &lt; &gt; &quot; &#39; &gt;</code></p>
<p>语法：<code>&lt;![CDATA[]]&gt;</code></p>
<p>案例，表示value值为A&amp;B需使用<code>&lt;![CDATA[A&amp;B]</code>&gt; 表示，或者使用特殊字符<code>A&amp;amp;B</code>来表示，其中的<code>&amp;amp;</code>则表示成特殊符号&amp;，还有其他的特殊符号，比如：</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>实体引用</th>
</tr>
</thead>
<tbody><tr>
<td><code>&lt;</code></td>
<td><code>&amp;lt;</code></td>
</tr>
<tr>
<td><code>&gt;</code></td>
<td><code>&amp;gt;</code></td>
</tr>
<tr>
<td><code>&amp;</code></td>
<td><code>&amp;amp;</code></td>
</tr>
<tr>
<td><code>&#39;</code></td>
<td><code>&amp;apos;</code></td>
</tr>
<tr>
<td><code>&quot;</code></td>
<td><code>&amp;quot;</code></td>
</tr>
</tbody></table>
<h2 id="1-5-ref-的-local-属性"><a href="#1-5-ref-的-local-属性" class="headerlink" title="1.5 ref 的 local 属性"></a>1.5 ref 的 local 属性</h2><ul>
<li>由于spring配置文件可拆分为多个（后续），使用local属性只能检索同当前配置文件中的 bean，比如：<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> = <span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;dao&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">ref</span> <span class="hljs-attr">local</span>=<span class="hljs-string">&quot;userDao&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="1-6-内部bean"><a href="#1-6-内部bean" class="headerlink" title="1.6 内部bean"></a>1.6 内部bean</h2><p><strong>使用案例：</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;dao&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span> = <span class="hljs-string">&quot;...&quot;</span>/&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>property标签里的bean只能被userService使用，而无法被其他的Bean引用</p>
<h2 id="1-7-注入list，set"><a href="#1-7-注入list，set" class="headerlink" title="1.7 注入list，set"></a>1.7 注入list，set</h2><ul>
<li><code>&lt;value&gt;</code> 、<code>&lt;ref&gt;</code> 标签注入<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span>&#123;<br>	List&lt;String&gt; hobbies;<br>	<span class="hljs-comment">//...</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-title function_">setHobbis</span><span class="hljs-params">(List&lt;String&gt; hobbies)</span>&#123;<span class="hljs-built_in">this</span>.hobbies=hobbies;&#125;<br>	<span class="hljs-comment">//...</span><br>&#125;<br></code></pre></td></tr></table></figure>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">class</span> =<span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span> = <span class="hljs-string">&quot;hobbies&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">list</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- 定义 list 或者数组中的元素 --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>睡觉<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>追剧<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">list</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">class</span> =<span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span> = <span class="hljs-string">&quot;hobbies&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">set</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- 定义 set 中的元素 --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>睡觉<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>追剧<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">set</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="1-8-注入map和properties属性"><a href="#1-8-注入map和properties属性" class="headerlink" title="1.8 注入map和properties属性"></a>1.8 注入map和properties属性</h2><p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span>&#123;<br>	Map&lt;String, String&gt; hobbies;<br>	<span class="hljs-comment">//...</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-title function_">setHobbis</span><span class="hljs-params">(Map&lt;String, String&gt; hobbies)</span>&#123;<span class="hljs-built_in">this</span>.hobbies=hobbies;&#125;<br>	<span class="hljs-comment">//...</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>方式一：通过map标签</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">class</span> =<span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span> = <span class="hljs-string">&quot;hobbies&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">map</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- 定义 Map 中的键值对 --&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">entry</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> <br>					<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>sleep<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>				<span class="hljs-tag">&lt;/<span class="hljs-name">key</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>睡觉<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">entry</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">entry</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> <br>					<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>see<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>				<span class="hljs-tag">&lt;/<span class="hljs-name">key</span>&gt;</span><br>				<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>追剧<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>			<span class="hljs-tag">&lt;/<span class="hljs-name">entry</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">map</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>方式二：通过props标签</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;user&quot;</span> <span class="hljs-attr">class</span> =<span class="hljs-string">&quot;...&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span> = <span class="hljs-string">&quot;hobbies&quot;</span>&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span><br>			<span class="hljs-comment">&lt;!-- 定义 Property中的键值对--&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;football&quot;</span>&gt;</span>足球<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>			<span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span> =<span class="hljs-string">&quot;basketball&quot;</span>&gt;</span>篮球<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>		<span class="hljs-tag">&lt;/<span class="hljs-name">props</span>&gt;</span><br>	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="1-9-注入null和空字符串"><a href="#1-9-注入null和空字符串" class="headerlink" title="1.9 注入null和空字符串"></a>1.9 注入null和空字符串</h2><p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span>&#123;<br>	<span class="hljs-keyword">private</span> String email;<br>	<span class="hljs-comment">//...</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-title function_">setEmail</span><span class="hljs-params">(String email)</span>&#123;<span class="hljs-built_in">this</span>.email=email;&#125;<br>	<span class="hljs-comment">//...</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>注入空值</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>  = <span class="hljs-string">&quot;email&quot;</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>注入null值</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml">&lt;/property name = &quot;email&quot;&gt;<br>	<span class="hljs-tag">&lt;<span class="hljs-name">null</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="二、AOP"><a href="#二、AOP" class="headerlink" title="二、AOP"></a>二、AOP</h1><hr>
<h2 id="2-1-异常抛出增强（通知advice）"><a href="#2-1-异常抛出增强（通知advice）" class="headerlink" title="2.1 异常抛出增强（通知advice）"></a>2.1 异常抛出增强（通知advice）</h2><p><strong>标签：</strong><code>&lt;aop:after-throing&gt;</code><br><strong>特点：</strong></p>
<ul>
<li>在目标方法抛出异常时织入增强处理</li>
<li>可拔插的异常处理方案<br><img src="https://img-blog.csdnimg.cn/fb4a5d94600b412ba51c0d80d1d176ce.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>
<p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ErrorLogger</span>&#123;<br>	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">log</span> <span class="hljs-operator">=</span> Logger.getLogger(ErrorLogger.class);<br><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">afterThrowing</span><span class="hljs-params">(JoinPoint jp, RuntimeException e)</span>&#123;<br>		log.error(jp.getSignature().getName() + <span class="hljs-string">&quot; 方法发生异常: &quot;</span> + e);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 声明增强方法所在的 Bean--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;theLogger&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;...&quot;</span>/&gt;</span><br><span class="hljs-comment">&lt;!-- 配置切面 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:config</span>&gt;</span><br>	<span class="hljs-comment">&lt;!--定义切入点 --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:pointcut</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;pointcut&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;execution(* service.Userservice.*(..))&quot;</span>/&gt;</span><br>	<span class="hljs-comment">&lt;!-- 引用包含增强方法的Bean --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:aspect</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;theLogger&quot;</span>&gt;</span><br>		<span class="hljs-comment">&lt;!-- 将afterThrowing()方法定义为异常抛出增强并引用pointcut切入点--&gt;</span><br>		<span class="hljs-comment">&lt;!-- 通过thowing属性指定名为e的参数注入异常实例--&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">aop:after-thowing</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;afterThrowing&quot;</span></span><br><span class="hljs-tag">			<span class="hljs-attr">pointcut-ref</span>=<span class="hljs-string">&quot;pointcut&quot;</span> <span class="hljs-attr">throwing</span>=<span class="hljs-string">&quot;e&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">aop:config</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-2-最终增强（通知）"><a href="#2-2-最终增强（通知）" class="headerlink" title="2.2 最终增强（通知）"></a>2.2 最终增强（通知）</h2><p><strong>标签：</strong><code>&lt;aop:after&gt;</code><br><strong>特点：</strong></p>
<ul>
<li>无论方法是否抛出异常，都会在目标方法最后织入增强处理，即：该增强都会得到执行</li>
<li>类似于异常处理机制中 <code>finally</code>块的作用，一般用于释放资源</li>
<li>可为各功能模块提供统一的，可拔插的处理方案</li>
</ul>
<p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AfterLogger</span>&#123;<br>	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">log</span> <span class="hljs-operator">=</span> Logger.getLogger(ErrorLogger.class);<br><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">afterLogger</span><span class="hljs-params">(JoinPoint jp)</span>&#123;<br>		log.error(jp.getSignature().getName() + <span class="hljs-string">&quot; 方法结束执行&quot;</span>);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 声明增强方法所在的 Bean--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;theLogger&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;...&quot;</span>/&gt;</span><br><span class="hljs-comment">&lt;!-- 配置切面 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:config</span>&gt;</span><br>	<span class="hljs-comment">&lt;!--定义切入点 --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:pointcut</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;pointcut&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;execution(* service.Userservice.*(..))&quot;</span>/&gt;</span><br>	<span class="hljs-comment">&lt;!-- 引用包含增强方法的Bean --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:aspect</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;theLogger&quot;</span>&gt;</span><br>		<span class="hljs-comment">&lt;!-- 将afterThrowing()方法定义为最终增强并引用pointcut切入点--&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">aop:after</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;afterLogger&quot;</span></span><br><span class="hljs-tag">			<span class="hljs-attr">pointcut-ref</span>=<span class="hljs-string">&quot;pointcut&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">aop:config</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-3-环绕增强"><a href="#2-3-环绕增强" class="headerlink" title="2.3 环绕增强"></a>2.3 环绕增强</h2><p><strong>标签：</strong><code>&lt;aop:around&gt;</code><br><strong>特点：</strong> </p>
<ul>
<li>目标方法前后都可以织入增强处理</li>
<li>功能<strong>最强大</strong>的增强处理</li>
<li>可获取或修改目标方法的参数、返回值，可对它进行异常处理，甚至可以决定目标方法<strong>是否执行</strong></li>
</ul>
<p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArroundLogger</span>&#123;<br>	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">log</span> <span class="hljs-operator">=</span> Logger.getLogger(ErrorLogger.class);<br><br>	<span class="hljs-keyword">public</span> Object <span class="hljs-title function_">aroundLogger</span><span class="hljs-params">(ProceedingJoinPoint jp)</span> <span class="hljs-keyword">throws</span> Throwable&#123;<br>		log.info(<span class="hljs-string">&quot; 调用 &quot;</span> + jp.getTarget() <span class="hljs-string">&quot; 的 &quot;</span> jp.getSignature().getName() + <span class="hljs-string">&quot; 方法，方法参数: &quot;</span> + Arrays.toString(jp.getArgs());<br>		<span class="hljs-keyword">try</span>&#123;<br>			<span class="hljs-type">Object</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> jp.proceed();	<span class="hljs-comment">//执行目标方法并获得返回值</span><br>			log.info(<span class="hljs-string">&quot; 调用&quot;</span> + jp.getTarget() + <span class="hljs-string">&quot; 的&quot;</span> + jp.getSignatrue().getName() + <span class="hljs-string">&quot; 方法，方法返回值: &quot;</span> + result);<br>			<span class="hljs-keyword">return</span> result;<br>		&#125; <span class="hljs-keyword">catch</span> (Throwable e)&#123;<br>			log.error(jp.getSignatrue().getName()+<span class="hljs-string">&quot; 方法发生异常: &quot;</span> + e);<br>			<span class="hljs-keyword">throw</span> e;<br>		&#125; finnaly&#123;<br>			log.info(jp.getSignature().getName() + <span class="hljs-string">&quot; 方法结束执行。&quot;</span>);<br>		&#125;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 声明增强方法所在的 Bean--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> =<span class="hljs-string">&quot;theLogger&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;...&quot;</span>/&gt;</span><br><span class="hljs-comment">&lt;!-- 配置切面 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:config</span>&gt;</span><br>	<span class="hljs-comment">&lt;!--定义切入点 --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:pointcut</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;pointcut&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;execution(* service.Userservice.*(..))&quot;</span>/&gt;</span><br>	<span class="hljs-comment">&lt;!-- 引用包含增强方法的Bean --&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">aop:aspect</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;theLogger&quot;</span>&gt;</span><br>		<span class="hljs-comment">&lt;!-- 将aroundLogger()方法定义为环绕增强并引用pointcut切入点--&gt;</span><br>		<span class="hljs-tag">&lt;<span class="hljs-name">aop:around</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;aroundLogger&quot;</span></span><br><span class="hljs-tag">			<span class="hljs-attr">pointcut-ref</span>=<span class="hljs-string">&quot;pointcut&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">aop:config</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="2-4-增强处理小结"><a href="#2-4-增强处理小结" class="headerlink" title="2.4 增强处理小结"></a>2.4 增强处理小结</h2><table>
<thead>
<tr>
<th align="left">增强处理类型</th>
<th align="left">特点</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Before</td>
<td align="left">前置增强处理，在目标方法前织入增强处理</td>
</tr>
<tr>
<td align="left">AfterReturning</td>
<td align="left">后置增强处理，在目标方法正常执行（不出现异常）后织入增强处理</td>
</tr>
<tr>
<td align="left">AfterThrowing</td>
<td align="left">异常增强处理，在目标方法抛出异常后植入增强处理</td>
</tr>
<tr>
<td align="left">After</td>
<td align="left">最终增强处理，不论方法是否抛出异常，都会在目标方法最后织入增强处理</td>
</tr>
<tr>
<td align="left">Around</td>
<td align="left">环绕增强处理，在目标方法的前后都可以织入增强处理</td>
</tr>
</tbody></table>
<h2 id="2-5-AOP配置标签总结"><a href="#2-5-AOP配置标签总结" class="headerlink" title="2.5 AOP配置标签总结"></a>2.5 AOP配置标签总结</h2><table>
<thead>
<tr>
<th align="left">AOP配置标签</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>&lt;aop:config&gt;</code></td>
<td align="left">AOP配置的顶层元素，大多数的<code>&lt;aop:*&gt;</code>元素必须包含在<code>&lt;aop:config&gt;</code>标签内</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:pointcut&gt;</code></td>
<td align="left">定义切点</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:aspect&gt;</code></td>
<td align="left">定义切面</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:after&gt;</code></td>
<td align="left">定义最终增强（不管被通知的方法是否执行成功）</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:after-returning&gt;</code></td>
<td align="left">定义 after-returning 增强</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:after-throwing&gt;</code></td>
<td align="left">定义 after-throwing 增强</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:around&gt;</code></td>
<td align="left">定义环绕增强</td>
</tr>
<tr>
<td align="left">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<code>&lt;aop:before&gt;</code></td>
<td align="left">定义前置增强</td>
</tr>
<tr>
<td align="left"><code>&lt;aop:aspectj-autoproxy&gt;</code></td>
<td align="left">启动@AspectJ注解驱动的界面</td>
</tr>
</tbody></table>
<h1 id="三、注解实现-IoC和AOP"><a href="#三、注解实现-IoC和AOP" class="headerlink" title="三、注解实现 IoC和AOP"></a>三、注解实现 IoC和AOP</h1><hr>
<h2 id="3-1-注解实现-IoC"><a href="#3-1-注解实现-IoC" class="headerlink" title="3.1 注解实现 IoC"></a>3.1 注解实现 IoC</h2><p>注解方式将 Bean 的定义信息和 Bean的实现类结合在一起，为配置文件 “瘦身” （开发人员常用），Spring核心提供的注解有：</p>
<table>
<thead>
<tr>
<th align="left">注解</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">@Component</td>
<td align="left">使用在类上用于实例化Bean</td>
</tr>
<tr>
<td align="left">@Controller</td>
<td align="left">使用在Web层上的类用于实例化Bean</td>
</tr>
<tr>
<td align="left">@Repository</td>
<td align="left">使用在dao层上的类用于实例化Bean</td>
</tr>
<tr>
<td align="left">@Service</td>
<td align="left">使用在service层上的类用于实例化Bean</td>
</tr>
<tr>
<td align="left">@Autowried</td>
<td align="left">对类成员变量、方法及构造函数进行标注，自动装配 Bean ，默认按数据类型，通常与@Qualifier搭配使用</td>
</tr>
<tr>
<td align="left">@Qualifier</td>
<td align="left">结合@Autowired一起使用，用于根据名称进行依赖注入</td>
</tr>
<tr>
<td align="left">@Resource</td>
<td align="left">相当于@Autowired+@Qualifier，按照名称进行注入</td>
</tr>
<tr>
<td align="left">@Value</td>
<td align="left">注入普通属性</td>
</tr>
</tbody></table>
<p>注：　在使用@Autowired时</p>
<ul>
<li>首先在容器中查询对应类型的bean</li>
<li>若查询结果刚好为一个，就将该bean装配给@Autowired指定的数据</li>
<li>若查询的结果不止一个，那么@Autowired会根据名称来查找。</li>
<li>若查询的结果为空，那么会抛出异常。解决方法时，使用<code>required=false</code></li>
</ul>
<p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Repository(&quot;userDao&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserDao</span>&#123;<br>	<span class="hljs-comment">//...</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>等效于：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span> = <span class="hljs-string">&quot;userDao&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;UserDaoImpl&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<p>使用Spring注解的前提，需要在Spring配置文件添加context，并启动<strong>注解扫描</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans</span></span><br><span class="hljs-string"><span class="hljs-tag">        http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="hljs-string"><span class="hljs-tag">        http://www.springframework.org/schema/context </span></span><br><span class="hljs-string"><span class="hljs-tag">        http://www.springframework.org/schema/context/spring-context.xsd&quot;</span></span><br><span class="hljs-tag">        &gt;</span><br>   <span class="hljs-comment">&lt;!-- 扫描包中注解标注的类 --&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.uni&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>使用案例：</strong><br>Dao层实现类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Repositry</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserDao</span>&#123;<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">save</span><span class="hljs-params">(User user)</span>&#123;<br>		System.out.println(<span class="hljs-string">&quot;JDBC save...&quot;</span>);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>业务层实现类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Service</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span>&#123;<br>	<span class="hljs-meta">@Autowried</span><br>	<span class="hljs-keyword">private</span> userDao userDao;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>测试类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">test</span><span class="hljs-params">()</span>&#123;<br>	<span class="hljs-type">ApplicationContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ClassPathXmlApplicationContext</span>(<span class="hljs-string">&quot;applicationContextAnnotation.xml`);</span><br><span class="hljs-string">	// 获取Bean</span><br><span class="hljs-string">	UserService userService = (UserService) context.getBean(&quot;</span>userServiceImpl”);<br>	userService.save(<span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>());<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>若不给注解指定name属性，默认的名称为类名（第一个字母小写）</li>
<li>@Autowried表示自动装配，默认按类型</li>
<li>若有相同类型的Bean会报错</li>
</ul>
<h3 id="3-1-1-Qualifier指定Bean名称"><a href="#3-1-1-Qualifier指定Bean名称" class="headerlink" title="3.1.1 @Qualifier指定Bean名称"></a>3.1.1 @Qualifier指定Bean名称</h3><p>在使用@Autowried注解实现Bean的自动装配时，默认按数据类型匹配，可以使用Qualifier指定Bean的名称，但是又出现了耦合。</p>
<p><strong>使用案例：</strong><br>对类的成员变量进行标注</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Service(&quot;userService&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span>&#123;<br>	<span class="hljs-meta">@Autowired</span><br>	<span class="hljs-meta">@Qualifier(&quot;userDao&quot;)</span><br>	<span class="hljs-keyword">private</span> UserDao dao;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>对方法的参数进行标注</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Service(&quot;userService&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span>&#123;<br>	<span class="hljs-keyword">private</span> UserDao dao;<br>	<span class="hljs-meta">@Autowired</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setDao</span><span class="hljs-params">((<span class="hljs-meta">@Qualifier(&quot;userDao&quot;)</span> UserDao dao)</span>&#123;<br>		<span class="hljs-built_in">this</span>.dao = dao;<br>	&#125;<br>	<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3-1-2-使用-Resource注解按名称自动装配"><a href="#3-1-2-使用-Resource注解按名称自动装配" class="headerlink" title="3.1.2 使用@Resource注解按名称自动装配"></a>3.1.2 使用@Resource注解按名称自动装配</h3><p><strong>使用案例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Service(&quot;userService&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">UserService</span>&#123;<br>	<span class="hljs-meta">@Resource(name=&quot;userDao&quot;)</span> <br>	<span class="hljs-keyword">private</span> UserDao dao; <span class="hljs-comment">// 为 dao属性注入名为userDao的Bean</span><br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="3-2-注解实现AOP"><a href="#3-2-注解实现AOP" class="headerlink" title="3.2 注解实现AOP"></a>3.2 注解实现AOP</h2><h3 id="3-2-1-使用注解定义切面"><a href="#3-2-1-使用注解定义切面" class="headerlink" title="3.2.1 使用注解定义切面"></a>3.2.1 使用注解定义切面</h3><p><strong>AspectJ：</strong>是面向切面的框架，扩展了Java语言，定义了AOP语法，能够在<strong>编译阶段</strong>提供代码的织入</p>
<p>Spring通过集成<code>AspectJ</code>实现了以注解的方式定义增强类，大大减少了配置文件中的工作量。</p>
<p>注：@AspectJ注解支持的JDK版本在5.0及以上。</p>
<p>使用前需在Spring配置文件中引入对该注解的支持：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">aop:aspectj-autoproxy</span> /&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>使用案例：</strong> 使用注解实现日志切面</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Component</span><br><span class="hljs-meta">@Aspect</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceLogger</span>&#123;<br>	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">log</span> <span class="hljs-operator">=</span> Logger.getLogger(UserServiceLogger.class);<br><br>	<span class="hljs-meta">@Pointcut(&quot;execution(* com.uni.service.*.*(..))&quot;)</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">pointCut</span><span class="hljs-params">()</span>&#123;&#125;<br>	<br>	<span class="hljs-meta">@Before(&quot;pointCut()&quot;)</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">before</span><span class="hljs-params">(JoinPoint jp)</span>&#123;<br>		log.info(<span class="hljs-string">&quot;调用 &quot;</span> + jp.getTarget() + <span class="hljs-string">&quot; 的 &quot;</span> + jp.getSignature().getName() + <span class="hljs-string">&quot; 方法, 方法参数: &quot;</span>  + Arryas.toString(jp.getArgs()));<br>	&#125;<br>	<span class="hljs-meta">@AfterReturning(value = &quot;pointCut()&quot;, returning=&quot;result&quot;)</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">afterReturning</span><span class="hljs-params">(JoinPoint jp, Object result)</span>&#123;<br>		log.info(<span class="hljs-string">&quot;调用 &quot;</span> + jp.getTarget() + <span class="hljs-string">&quot; 的&quot;</span> + jp.getSignature().getName() + <span class="hljs-string">&quot; 方法，方法返回值: &quot;</span> + result);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-3-使用注解定义增强"><a href="#3-3-使用注解定义增强" class="headerlink" title="3.3 使用注解定义增强"></a>3.3 使用注解定义增强</h2><p>@AfterThrowing注解定义异常抛出增强<br>@Ater注解定义异常抛出增强<br>@Around注解定义异常抛出增强<br>…</p>
<h2 id="3-4-Spring切面配置小结"><a href="#3-4-Spring切面配置小结" class="headerlink" title="3.4 Spring切面配置小结"></a>3.4 Spring切面配置小结</h2><ul>
<li>使用注解方式定义切面可以简化配置的工作量</li>
<li>常用注解有@Aspect、@Before、@AfterReturning、@Around、@AfterThowing、@After等</li>
<li>在配置文件中添加<code>&lt;aop:aspectj-autoproxy/&gt;</code>标签，启用对@AspectJ注解的支持</li>
<li>@Aspecj支持的JDK版本在5.0及以上</li>
</ul>
<h1 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a>四、总结</h1><hr>
<p><strong>Ioc的不同实现方式</strong></p>
<p>注入途径：设值注入 &#x2F; 构造注入<br>配置方法：XML的p命名空间  &#x2F; 注解方式</p>
<p><strong>IoC注入不同类型的数据</strong></p>
<ul>
<li>前置增强</li>
<li>后置增强</li>
<li>异常抛出增强</li>
<li>环绕增强</li>
<li>最终增强</li>
</ul>
<p><strong>AOP增强类型</strong></p>
<ul>
<li>POJO增强类 + <code>&lt;aop:xxx&gt;</code></li>
<li>注解定义增强类 + <code>&lt;aop:aspectj-autoproxy&gt;</code></li>
</ul>
<p><strong>AOP注解配置方法</strong></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Spring/" class="category-chain-item">Spring</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Spring/">#Spring</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP</div>
      <div>http://example.com/2022/01/20/Spring 笔记2 依赖注入的多种方式构造注入、p命名空间注入等 _ AOP多种增强类型异常抛出增强、环绕增强等 _ 注解开发实现 IoC_DI 和 AOP/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年1月20日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/20/Hadoop%20_%20MapReduce%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89Hadoop%E5%BA%8F%E5%88%97%E5%8C%96%E6%93%8D%E4%BD%9C%20_%20%E6%A1%88%E4%BE%8B%20_%20%E5%B0%81%E8%A3%85map%E5%92%8Creduce%E6%93%8D%E4%BD%9C%E7%9A%84value%E7%B1%BB%E5%9E%8B/" title="Hadoop _ MapReduce学习笔记（二）Hadoop序列化操作 _ 案例 _ 封装map和reduce操作的value类型">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Hadoop _ MapReduce学习笔记（二）Hadoop序列化操作 _ 案例 _ 封装map和reduce操作的value类型</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/19/Hadoop%20_%20MapReduce%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%80%EF%BC%89%E4%BD%BF%E7%94%A8Java%E5%AE%9E%E7%8E%B0MapReduce%E7%BC%96%E7%A8%8B%20_%20%E6%89%93%E5%8C%85%E8%AF%8D%E9%A2%91%E7%BB%9F%E8%AE%A1%E7%A8%8B%E5%BA%8F%E4%B8%BAjar%E5%8C%85%E6%8F%90%E4%BA%A4%E5%88%B0Hadoop%E9%9B%86%E7%BE%A4%E5%B9%B6%E8%BF%90%E8%A1%8C%20_%20Mapper%E3%80%81Reducer%E3%80%81Driver/" title="Hadoop _ MapReduce学习笔记（一）使用Java实现MapReduce编程 _ 打包词频统计程序为jar包提交到Hadoop集群并运行 _ Mapper、Reducer、Driver">
                        <span class="hidden-mobile">Hadoop _ MapReduce学习笔记（一）使用Java实现MapReduce编程 _ 打包词频统计程序为jar包提交到Hadoop集群并运行 _ Mapper、Reducer、Driver</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
